import { getInfo, setInfo } from '@/utils/storage'
import { logout } from '@/api/login'

const getInitialState = () => ({
  // 个人权证
  userInfo: getInfo(),

  // 存放本地验证码信息
  userPicCodeId: null
})

export default {
  namespaced: true,
  state: getInitialState(),
  mutations: {
    setUserInfo (state, obj) {
      state.userInfo = obj
      setInfo(obj)
    },

    // 更新验证码图片id
    setUserPicCodeId (state, obj) {
      state.userPicCodeId = obj
    },

    logout () {
      this.$dialog.confirm({
        title: '温馨提示',
        message: '你确认要退出么？'
      })
        .then(() => {
          this.$store.dispatch('user/logout')
        })
        .catch(() => {
          console.log('退出失败')
        })
    }
  },
  actions: {
    async logout (context) {
      await logout()
      context.commit('setUserInfo', {})
      context.commit('cart/setCartList', [], { root: true })
      // 调用address仓库方法重置数据源
      context.commit('address/setAddressList', [], { root: true })
    }
  },
  getters: {}
}
